;
(function (call) {
    var main = {
        userInfo: null,
        mapId: "jcybMap",
        init: function () {
            this.bindEvt();
            this.initView();
        },
        initView: function () {
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
			
            this.getAreaData();
            $("#modal_picture").find("#modal-title span").first().click();
			if (this.userInfo.roleIds.indexOf('e9b0eaab-f2a3-4f71-9e64-644b255a706c') > -1) {
                $(".pic-upload").hide();
            }
        },
        getAreaData: function () {
            var that = this;
            $.getJSON(api.internal.business.getAreaData, function (res) {
                if (res.isSuccess) {
                    var data = res.data;
                    that.dataDisplay(data);
                    that.dataUpload(data)
					
					if (that.userInfo.roleIds.indexOf('e9b0eaab-f2a3-4f71-9e64-644b255a706c') > -1) {
						$(".excel-down i").hide();
					}
                }
            });
        },
        // 数据展示页面
        dataDisplay: function (data) {
            var $excel = $('.excel-down');
            for (var i = 0; i < data.length; i++) {
                var areaObj = data[i];
                if (areaObj.areaId == 12) continue;
                var $li = $('<li><span>' + areaObj.areaName + '</span><i>下载</i></li>');
                if (i === 0)
                    $li.find('span').addClass('active');
                $li.data('area', areaObj);
                $excel.append($li);
            }
        },
        // 展示数据上传页面
        dataUpload: function (data) {
            var that = this;
            var $company = $('#company');
            $(".upload-people .people").text(that.userInfo.smuser.name);
            for (var i = 0; i < data.length; i++) {
                var areaObj = data[i];
				if (areaObj.areaId == 12) continue;
                var code = Number(that.userInfo.userAreaCode);
                if (code === areaObj.areaId) {
                    $company.append(`<option selected value=${areaObj.areaId}>${areaObj.areaName}</option>`);
                } else {
                    $company.append(`<option value=${areaObj.areaId}>${areaObj.areaName}</option>`);
                }
                if (code === 12) {
                    $company.removeAttr('disabled');
                } else {
                    $company.attr('disabled', 'disabled');
                }
            }
        },
        // 上传
        upload: function () {
            var areaCode = $("#company").val();
            var name = $("#excel-file").val();
            if (name == '') {
                alert('请选择需要上传的图片');
                return
            }
            var forData = new FormData();
            forData.append('file', $('#excel-file')[0].files[0]);
            loading.add('#modal_picture');
            $.ajax({
                url: api.internal.business.yztUpload + areaCode,
                type: "POST",
                data: forData,
                dataType: 'json',
                cache: false,
                processData: false,
                contentType: false,
                success: function (res) {
                    loading.remove();
                    if (res.isSuccess) {
                        alert("上传成功");
                        $("#file-text").text('').removeAttr('title');
                        $("#excel-file").val('')
                    } else {
                        alert("上传失败");
                    }
                }
            })
        },
        // 图片预览弹窗
        preview: function (areaCode) {
            var that = this;
            $("#modal_picture_Preview").remove()
            CafeJS.ModalBox({
                id: 'picture_Preview',
                title: "图片预览",
                width: 1250,
                height: 750,
                m_top: 70,
                m_left: 335,
                hide: false,
                createSucc: function () {
                    that.picturePreview(areaCode);
                }
            });
        },
        // 预览图片
        picturePreview: function (areaCode) {
            var param = {
                areaCode: areaCode
            }
            $.getJSON(api.internal.business.yztlook + areaCode, param, function (res) {
                if (res.isSuccess) {
                    var img = res.data;
                    $("#modal_picture_Preview .modal-body").empty().append(`<img id='areaImg' src="${img}">`);
                } else {
					alert("没有可预览的图片");
					CafeJS.ModalBoxClose('picture_Preview');
				}
            })
        },
        downloadPicture: function (areaCode) {
            var param = {
                areaCode: areaCode
            }
            $.getJSON(api.internal.business.yztDownLoad + areaCode, param, function (res) {
                if (res.isSuccess) {
                    var img = res.data;
                    $("#downLoadImg").attr('href', img)
                    $("#downLoadImg")[0].click()
                }
            })
        },
        zoomImg: function (o) {
            var zoom = parseInt(o.style.zoom, 10) || 100;
            zoom += event.wheelDelta / 2; //可适合修改
            if (zoom > 0) o.style.zoom = zoom + '%';
        },
        bindEvt: function () {
            var that = this;
            $("#modal_picture").find(".modal-title>span").click(function () {
                $(this).addClass("active").siblings().removeClass("active");
                $(".picture-content>li").eq($(this).index()).show().siblings().hide();
            });
            $("#modal_picture .close").click(function () {
                $("#modal_picture").remove();
                $("#picture").removeClass("active");
            });
            $(".excel-down").on('click', 'li span', function () {
                var areaCode = $(this).parent().data('area').areaId;
                that.preview(areaCode)
            }).on('click', 'li i', function () {
                var areaCode = $(this).parent().data('area').areaId;
                var url = api.internal.business.yztDownLoad + areaCode
                $("#imgIframe").attr('src', url)
            })
            $("#excel-file").change(function () {
                var val = $("#excel-file").val()
                $("#file-text").text(val).attr('title', val)
            })
            $('.upload-btn').click(function () {
                that.upload()
            })
            $("body").on("mousewheel", '#areaImg', function () {
                that.zoomImg(this);
                return false;
            });

            var canMove = false;
            var x = 0;
            var y = 0;
            $('body').on('mousedown', '#areaImg', function (e) {
                var img = document.getElementById("areaImg");
                canMove = true;
                x = e.pageX - img.offsetLeft;
                y = e.pageY - img.offsetTop;
            })
            window.onmouseup = function (e) {
                canMove = false;
            }
            window.onmousemove = function (e) {
                e.preventDefault();
                var img = document.getElementById("areaImg");
                if (canMove) {
                    img.style["left"] = (e.pageX - x) + "px";
                    img.style["top"] = (e.pageY - y) + "px";
                }
            }
        },
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    main.init();
});